from WriteDocuments import FAISSService
import pymysql

# 数据库配置
host = '10.240.53.162'
port = 3306
user = 'rdsroot'
password = 'Geely@db20211206'
db_name = 'test'
charset = 'utf8mb4'  # 字符集设置

faiss = FAISSService()
try:
    # 使用with语句管理连接和游标
    with pymysql.connect(host=host, port=port, user=user, password=password,
                         db=db_name, charset=charset) as db:
        with db.cursor(cursor=pymysql.cursors.DictCursor) as cursor:
            cursor.execute(
                """select * from user_training where state='Untrained' """)
            allRow = cursor.fetchall()
            for row in allRow:
                print(row["content"])
                faiss.write_templates(row["content"])
            cursor.execute(
                """UPDATE `user_training` SET `state` = 'Trained' where state='Untrained' """)
            db.commit()

except pymysql.MySQLError as e:
    print("MySQL错误:", e)
